package com.blog4j.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.blog4j.api.vo.OssBaseConfigVo;
import com.blog4j.common.constants.CacheConstants;
import com.blog4j.common.enums.ErrorEnum;
import com.blog4j.common.exception.Blog4jException;
import com.blog4j.system.entity.OssBaseConfigEntity;
import com.blog4j.system.entity.OssExtendConfigEntity;
import com.blog4j.system.mapper.OssBaseConfigMapper;
import com.blog4j.system.service.OssBaseConfigService;
import com.blog4j.system.service.OssExtendConfigService;
import com.blog4j.system.vo.req.SaveOssBaseConfigReqVo;
import com.blog4j.system.vo.req.SaveOssExtendConfigReqVo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @author 98k灬
 * @version v1.0.0
 * @Description : 功能描述
 * @Create on : 2024/7/19 22:15
 **/
@Service
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class OssBaseConfigServiceImpl extends ServiceImpl<OssBaseConfigMapper, OssBaseConfigEntity>
        implements OssBaseConfigService {
    private final OssExtendConfigService ossExtendConfigService;

    /**
     * 获取OSS基础配置信息
     *
     * @return OSS基础配置信息
     */
    @Cacheable(value = CacheConstants.SYSTEM, key = " 'ossBaseConfig' ")
    @Override
    public OssBaseConfigVo getOssBaseConfig() {
        OssBaseConfigEntity ossBaseConfig = this.baseMapper.selectOne(null);
        OssBaseConfigVo ossBaseConfigVo = new OssBaseConfigVo();
        BeanUtils.copyProperties(ossBaseConfig, ossBaseConfigVo);
        return ossBaseConfigVo;
    }

    /**
     * 保存OSS基础配置信息
     *
     * @param reqVo 请求信息
     */
    @CacheEvict(value = CacheConstants.SYSTEM, key = " 'ossBaseConfig' ")
    @Override
    public void saveOssBaseConfig(SaveOssBaseConfigReqVo reqVo) {
        Integer id = reqVo.getId();
        OssBaseConfigEntity ossBaseConfig = this.baseMapper.selectById(id);
        if (Objects.isNull(ossBaseConfig)) {
            throw new Blog4jException(ErrorEnum.OSS_BASE_CONFIG_NOT_EXIST_ERROR);
        }

        BeanUtils.copyProperties(reqVo, ossBaseConfig);
        this.baseMapper.updateById(ossBaseConfig);
    }

    /**
     * 获取OSS扩展配置信息
     *
     * @return OSS基础配置信息
     */
    @Cacheable(value = CacheConstants.SYSTEM, key = " 'ossExtendConfig' ")
    @Override
    public OssExtendConfigEntity getOssExtendConfig() {
        return ossExtendConfigService.getOne(null);
    }

    /**
     * 保存OSS扩展配置信息
     *
     * @param reqVo 请求信息
     */
    @CacheEvict(value = CacheConstants.SYSTEM, key = " 'ossExtendConfig' ")
    @Override
    public void saveOssExtendConfig(SaveOssExtendConfigReqVo reqVo) {
        OssExtendConfigEntity ossExtendConfig = ossExtendConfigService.getById(reqVo.getId());
        if (Objects.isNull(ossExtendConfig)) {
            throw new Blog4jException(ErrorEnum.OSS_EXTEND_CONFIG_NOT_EXIST_ERROR);
        }
        BeanUtils.copyProperties(reqVo, ossExtendConfig);
        ossExtendConfigService.updateById(ossExtendConfig);
    }
}
